/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.loja.view.swing;

import java.awt.Component;
import java.awt.Dimension;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import javax.persistence.GeneratedValue;
import javax.persistence.Transient;
import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import br.com.loja.model.entity.Switchable;
import br.com.loja.model.service.factory.ServiceFactory;
import br.com.loja.util.reflection.ReflectionUtil;
import br.com.loja.view.swing.model.FormField;

import com.jgoodies.forms.builder.DefaultFormBuilder;
import com.jgoodies.forms.layout.FormLayout;

/**
 *
 * @author DavidLuiz
 */
public class PanelEntity extends JPanel {

    private final ServiceFactory serviceFactory;

    public PanelEntity(ServiceFactory serviceFactory) {
        this.serviceFactory = serviceFactory;
    }

    public JPanel getPanel(Class<? extends Switchable> entityClass) {

        DefaultFormBuilder builder = new DefaultFormBuilder(new FormLayout(""));
        builder.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
        builder.appendColumn("right:pref");
        builder.appendColumn("3dlu");
        builder.appendColumn("fill:max(pref; 180px)");

        builder.appendSeparator(entityClass.getSimpleName());

        for (Field f : ReflectionUtil.getAllFields(entityClass)) {

            if (!isMappable(f)) {
                continue;
            }

            ComponentFactoryOld factory = new ComponentFactoryOld(f, serviceFactory);
            Component component = factory.getComponent();

            if (component != null) {
                if (component instanceof JTable) {
                    JScrollPane scrollPane = new JScrollPane();
                    scrollPane.setName("List " + component.getName());
                    scrollPane.setPreferredSize(new Dimension(180, 240));
                    scrollPane.setViewportView(component);
                    builder.nextLine();
                    builder.append(scrollPane.getName(), scrollPane);

                } else {
                    builder.nextLine();
                    builder.append(component.getName(), component);
                }
            }
        }

        return builder.getPanel();

    }

    private static boolean isMappable(Field f) {

        if (Modifier.isStatic(f.getModifiers()) || Modifier.isTransient(f.getModifiers())) {
            return false;
        }
        Annotation annotations[] = f.getAnnotations();

        for (Annotation a : annotations) {
            if (a instanceof Transient) {
                return false;
            }
            if (a instanceof FormField) {
                return false;
            }
            if (a instanceof GeneratedValue) {
                return false;
            }
        }

        return true;
    }

}
